package com.ganzi.ganziserver.service.imp;

import com.ganzi.ganziserver.core.*;
import com.ganzi.ganziserver.dao.CollectInformationDao;
import com.ganzi.ganziserver.dao.ControlLogDao;
import com.ganzi.ganziserver.dao.FanInfromationDao;
import com.ganzi.ganziserver.dao.SchoolInformationDao;
import com.ganzi.ganziserver.service.ControlLogService;
import com.ganzi.ganziserver.shiro.redis.OtherCache.UserCache;
import com.ganzi.ganziserver.utils.GetTime;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class ControlLogServiceImp implements ControlLogService{

    private static Logger log = LoggerFactory.getLogger(ControlLogServiceImp.class);

    @Autowired
    ControlLogDao logDao;
    @Autowired
    FanInfromationDao fanDao;
    @Autowired
    SchoolInformationDao schoolDao;
    @Autowired
    CollectInformationDao collectInformationDao;
    @Autowired
    UserCache cache;

    @Override
    public void saveLogForControl(long fanId, String type, String value,int code,String stateInformation,String infoId) throws Exception {
        if(fanId != 0){
            FanInformation fanInformation = fanDao.findFanInformationById(fanId);
            if(fanInformation != null){
                long uid = cache.getUserIdByPrincipal();
                String schoolName = null;
                SchoolInformation school = schoolDao.findSchoolInformationById(fanInformation.getSchoolId());
                if(school != null)schoolName = school.getSchoolName();
                ControlLog log = ControlLog.builder().userId(uid).schoolId(fanInformation.getSchoolId())
                        .schoolName(schoolName).collectNumber(fanInformation.getCollectNumber()).fanNumber(fanInformation.getFanNumber())
                        .sendType(0).controlType(Integer.parseInt(type)).sendState(code).information(value).stateInformation(stateInformation)
                        .createTime(GetTime.getNowTimeForLong()).infoId(infoId).build();
                logDao.insertControlLog(log);
            }else{
                log.info("fanInformation never existed");
            }
        }else{
            log.info("fanid is null");
        }

    }

    @Override
    public void saveLogForQuery(int code, String stateInformation, String infoId, long schoolId, OtherInformation other,int type,String value) throws Exception {
        long uid = cache.getUserIdByPrincipal();
        String SchoolName = "全部学校";
        if(other != null){
            SchoolName = other.getSchoolName();
        }
        ControlLog log = ControlLog.builder().userId(uid).schoolId(schoolId)
                .schoolName(SchoolName).collectNumber(0).fanNumber(0)
                .sendType(1).controlType(type).sendState(code).information(value).stateInformation(stateInformation)
                .createTime(GetTime.getNowTimeForLong()).infoId(infoId).build();
        logDao.insertControlLog(log);
    }

    @Override
    public List<ControlLog> findLog(long userId, long schoolId, int sendState, long beforeTime, long afterTime) throws Exception{
        return logDao.findControlLog(userId,schoolId,sendState, beforeTime,afterTime);
    }

    @Override
    public int findLogCount(long userId, long schoolId, int sendState, long beforeTime, long afterTime) throws Exception {
        return logDao.findControlLogCount(userId,schoolId,sendState, beforeTime,afterTime);
    }

    @Override
    public List<CollectInformation> findCollectInformation(long fanId, long beforTime, int size) throws Exception {
        return collectInformationDao.findCollectInformationByFanId(fanId,beforTime,size);
    }


}
